import Default from "@/store/default.ts";
import { DataLocal, Menu } from "@/store/interface.ts";

//传来的值
export let dataLocal: DataLocal;

//资源地址
export let resources: string;

// 在开发环境中使用 VITE_DATA_LOCAL 环境变量
if (import.meta.env.VITE_APP_ENV === "development") {
  //dataLocal = JSON.parse(import.meta.env.VITE_DATA_LOCAL);
  dataLocal = Default;
  resources = ""; //图片地址
}
// 在生产环境中使用真实数据源的相关配置
else {
  dataLocal = (window as any).dataLocal;
  //资源地址
  resources = dataLocal.site.theme + "/vite/dist"; //图片地址
}

//向数组中添加状态控制并生成新数组
export const addActive = (datas: Menu[]) => {
  return datas.map(function (obj: Menu) {
    return { ...obj, active: false };
  });
};

//控制子组件展开状态
export const toggleAccordion = (accordions: Menu[], index: number) => {
  accordions.forEach((accordion: { active?: boolean }, i: number) => {
    if (i === index) {
      //accordion.active = !accordion.active; // 切换当前子菜单的状态
      accordion.active = true; // 切换当前子菜单的状态
    } else {
      accordion.active = false; // 关闭其他子菜单
    }
  });
};

//控制子组件展开状态 - 移动端专用

//将传来的值保存在浏览器中
export const addLocal = (data: Array<string>) => {
  const sum = JSON.stringify(data);
  localStorage.setItem("menuPositionArray", sum);
};
//读取值
export const readLocal = () => {
  const data = localStorage.getItem("menuPositionArray");
  const parsedData = data ? JSON.parse(data) : ["-1", "-1"];
  return parsedData;
};

//通过存储的值修改菜单状态
export const modify = (array: Menu[], num: string) => {
  const obj = array.find((item) => item.id === num); //找到指定ID的对象
  if (obj) {
    obj.active = true;
  } else {
    //没找到有指定ID的
    array[0].active = true;
  }
};

// 简化判断逻辑的函数
//浏览器本地拿到的ID  默认菜单，传来的ID
export const getSizeColor = (
  activeId: string,
  defaultMenu: Menu[],
  id: string
) => {
  const idMenu = defaultMenu[0].list[0].id; //默认子菜单ID
  const f =
    (activeId === "-1" && idMenu === id) || activeId === id
      ? "rgb(214, 17, 17)"
      : "";
  return { color: f };
};
